package com.lwl.blue.commont.utils;

import com.lwl.blue.entity.Item;

import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * <p>
 * 单位转换工具类
 * </p>
 *
 * @version V0.0.1
 * @date 2023/11/11
 */
public class UnitChangeUtil {

    /**
     * 增加单位转换数据
     */
    /**
     * @param data          数据
     * @param dataClass     类class
     * @param nameSuffix    后缀
     * @param multiple      运算 数据
     * @param operationType 远程规则
     */
    /**
     *
     * @param data          数据
     * @param dataClass     类class
     * @param nameSuffix    后缀
     * @param multiple      运算 数据
     * @param operationType 远程规则
     * @param scale  小数位数
     */
    public static void getUnitChangeData(Object data, Class dataClass, String nameSuffix, BigDecimal multiple, String operationType, int scale) {
        Field[] declaredFields = dataClass.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String name = field.getName();
            if (name.endsWith(nameSuffix)) {
                try {
                    String getName = name.substring(0, name.lastIndexOf(nameSuffix));
                    Field gField = dataClass.getDeclaredField(getName);
                    gField.setAccessible(true);
                    Object value = gField.get(data);
                    if (value instanceof BigDecimal) {
                        BigDecimal a = (BigDecimal) value;
                        if (null != a) {
                            BigDecimal b = null;
                            switch (operationType) {
                                case "DIVIDE"://除法
                                    b = a.divide(multiple,scale).setScale(scale,BigDecimal.ROUND_HALF_UP);
                                    break;
                                case "ADD":// 加
                                    b = a.add(multiple).setScale(scale);
                                    break;
                                case "MULTIPLY"://乘
                                    b = a.multiply(multiple).setScale(scale);
                                    break;
                                case "subtract": //减
                                    b = a.subtract(multiple).setScale(scale);
                                default:
                                    break;
                            }
                            if (b != null) {
                                field.set(data, b);
                            }
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * cm 转 英寸
     *
     * @param object
     * @param oClass
     */
    public static void getInchDate(Object object, Class oClass) {
        getUnitChangeData(object, oClass, "Inch", new BigDecimal("2.54"), "DIVIDE", 2);
    }

    /**
     * 公斤转磅
     *
     * @param object
     * @param oClass
     */
    public static void getBlData(Object object, Class oClass) {
        getUnitChangeData(object, oClass, "Bl", new BigDecimal("2.2"), "MULTIPLY", 2);
    }

    public static void main(String[] args) {
        Item item = new Item();
        item.setBoxSizeHeight(new BigDecimal("1.35"));
        item.setBoxSizeLength(new BigDecimal("1.22"));
        UnitChangeUtil.getInchDate(item, Item.class);
        BigDecimal boxSizeHeightInch = item.getBoxSizeHeightInch();
        BigDecimal innerBoxSizeLengthInch = item.getBoxSizeLengthInch();
        int i = 0;
    }
}
